home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / visual / perl.exe / {app} / Plug-Ins / PlugInDemo.pl < prev    next >
Encoding:
Text File  |  2003-04-15  |  11.5 KB  |  390 lines

  1. #Name: PlugIn Demo
  2. #Description: Nonsense demo that just uses and explains every OLE method (\optiperl\plug-ins\PlugInDemo.pl)
  3. #Icon: %opti%Tools.icl,146
  4. #Extensions: NewProcess
  5.  
  6. #Note the 3 comments above. These are *absolutely* necessary for each
  7. #plug in. Also since we are using TK, the 4th line is also necessary.
  8. #These lines are parsed when OptiPerl loads or by selecting menu item
  9. #Tools / Plug-ins / Update plugins.
  10.  
  11. use Tk;
  12. use Win32::OLE;
  13.  
  14. #Now we will write our code. Notice that *all* our code is enclosed in subroutines.
  15. #Do not add code outside subroutines.
  16.  
  17. #If you are using TK, this also includes *not* calling the
  18. #MainLoop;
  19. #sub anywhere. OptiPerl wfill take care of calling TK's message handler.
  20.  
  21.  
  22. sub Initialization {
  23. #The Initialization subroutine is called when the plug-in starts.
  24. #usage: Initialization(int PlugID)
  25.  
  26.  $plug_id = $_[0];
  27.  #The first parameter is a handle to the plug-in. We will need this
  28.  #for future commands
  29.  
  30.  $optiperl = Win32::OLE->new('OptiPerl.Application');
  31.  #Initialize OLE object
  32.  
  33.  $main = MainWindow->new;
  34.  #Initialize TK toplevel window
  35.  
  36.  $main->Label(-text => 'OptiPerl Plug-in Demo')->pack;
  37.  #Add a label
  38.  
  39.  $log = $main->Listbox()->pack;
  40.  AddLog("Initialization");
  41.  
  42.  $main->OnDestroy(sub{Finalization()});
  43.  #IMPORTANT: Add an event handler when TK destroys itself to call
  44.  #the Finalization subroutine.
  45.  
  46.  $win = $optiperl->RequestWindow($plug_id);
  47.  #Request a dockable window in OptiPerl
  48.  
  49.  if (defined $win) {
  50.   $win->Show;
  51.   #Show the window
  52.  
  53.   $win->{Title} = "Plug-in Demo";
  54.   #set the windows title
  55.  
  56.   ProcessEvents();
  57.   #"Magic" subroutine in plugins to force TK to process
  58.   #all windows events to create the window.
  59.  
  60.   $optiperl->DockWindow($plug_id, hex $main->winfo("id"), $win);
  61.   #Dock TK's toplevel window into OptiPerl
  62.  
  63.   # The following code creates our menu system and toolbars
  64.  
  65.   # create a submenu.
  66.   my $menu = $optiperl->CreateToolItem($plug_id,"menu");
  67.   #set it's settings
  68.   $menu->SetOptions("Tests","","","","");
  69.  
  70.   # create buttons. These will be items in the menu and also linked
  71.   # in the toolbars.
  72.   my $btn = $optiperl->CreateToolItem($plug_id,"button");
  73.   # set the buttons options.
  74.   # SetOptions(Caption, Hint, Image, Shortcut, On Click subroutine name)
  75.   $btn->SetOptions(
  76.       "Application Test","Test IApplication","%opti%tools.icl,117",
  77.       "Alt+F5","IApplication_Test");
  78.   # Note that setoptions is a fast way to set all the settings of
  79.   # a tool item. We could also do:
  80.   # $btn->{Enabled}=1;
  81.   # $btn->{Hint}="test";
  82.   # $btn->{Caption}="caption";
  83.   # $btn->{Image}="cool.dll,100";
  84.   # $btn->{Visible}=1;
  85.   # $btn->{Shortcut}="Ctrl+Shift+F1";
  86.   # $btn->{OnClick}="SubName";
  87.   $menu->ToolLinks->Add($btn,0);
  88.  
  89.   $btn = $optiperl->CreateToolItem($plug_id,"button");
  90.   $btn->SetOptions(
  91.       "IDocument Test","Test IDocument","%opti%tools.icl,4",
  92.       "Alt+F6","IDocument_Test");
  93.   $menu->ToolLinks->Add($btn,0);
  94.  
  95.   $btn = $optiperl->CreateToolItem($plug_id,"button");
  96.   $btn->SetOptions(
  97.       "IProject Test","Test IProject","%opti%tools.icl,28",
  98.       "Alt+F7","IProject_Test");
  99.   $menu->ToolLinks->Add($btn,0);
  100.  
  101.   $btn = $optiperl->CreateToolItem($plug_id,"button");
  102.   $btn->SetOptions(
  103.       "ICodeExplorer Test","Test ICodeExplorer","%opti%tools.icl,100",
  104.       "Alt+F8","ICodeExplorer_Test");
  105.   $menu->ToolLinks->Add($btn,0);
  106.  
  107.   $btn = $optiperl->CreateToolItem($plug_id,"button");
  108.   $btn->SetOptions(
  109.       "Window Test","Test Window","%opti%tools.icl,137",
  110.       "Alt+F9","Window_Test");
  111.   $menu->ToolLinks->Add($btn,0);
  112.  
  113.   $btn = $optiperl->CreateToolItem($plug_id,"button");
  114.   $btn->SetOptions(
  115.       "Big Test","","%opti%tools.icl,134","Big Test","Big_Test");
  116.   $menu->ToolLinks->Add($btn,1);
  117.  
  118.   $btn = $optiperl->CreateToolItem($plug_id,"button");
  119.   $btn->SetOptions(
  120.       "Exit","Close plug-in","%opti%tools.icl,1",
  121.       "","Finalization");
  122.   $menu->ToolLinks->Add($btn,1);
  123.  
  124.   $win->MainBarLinks->Add($menu,0);
  125.   #add the menu to the menu of the window
  126.  
  127.   $win->PopUpLinks->AssignLinks($menu->ToolLinks);
  128.   #add the menu to the pop-up menu (when the arrow is pressed)
  129.  
  130.   $optiperl->ToolBarLinks($plug_id)->AssignLinks($menu->ToolLinks);
  131.   #add the menu to the main toolbar assigned to this plugin
  132.   #(the one created next to the main menu)
  133.  
  134.   $optiperl->UpdateToolBars($plug_id);
  135.   #Necessary! Error will occur if the above is not called after
  136.   #messing around with the toolbars.
  137.   $optiperl->ToolBarVisible($plug_id,1);
  138.   $win->ReDraw;
  139.  }
  140.  
  141.  # MessageBoxTest();
  142. }
  143.  
  144.  
  145. sub Finalization {
  146. #The Finalization sub is called when the plug-in terminates. If you press the
  147. #close button we created above, this will be called. However it will also be called
  148. #if the user closes manually this plug-in within optiperl.
  149.  
  150.  #Note: Don't pop-up any dialogs here asking questions like "do you want to
  151.  #save changes" etc. These go in the OnCanTerminate subroutine below.
  152.  
  153.  $optiperl->CodeExplorer->DeleteNode($newnode);
  154.  
  155.  #Restore status bar
  156.  $optiperl->StatusBarRestore;
  157.  if (defined $win2) {
  158.   $optiperl->DestroyWindow($plug_id,$win2);
  159.   $win2->Hide;
  160.  }
  161.  $optiperl->DestroyWindow($plug_id,$win);
  162.  $win->Hide;
  163.  $optiperl->EndPlugIn($plug_id);
  164. }
  165.  
  166. sub OnCanTerminate {
  167.  AddLog("OnCanTerminate");
  168.  #OnCanTerminate is called when the user wants to close OptiPerl.
  169.  #If you use this subroutine, return true or false for whether optiperl
  170.  #should be allowed to close.
  171.  
  172.  #For example here you could show a dialog with the text
  173.  #"Would you like to save changes? " (with yes, no & cancel buttons).
  174.  #If the user pressed YES, your scripts would save changes and
  175.  #return 1; NO would ignore changes and return 1; CANCEL would
  176.  #return 0.
  177.  
  178.  #Note that if you are going to return 1 to let optiperl exit, still don't
  179.  #add any code that will uninitialize your script; because the user might
  180.  #be running another plug-in that will not let optiperl terminate.
  181.  #Any code to uninitialize your script goes into the Finalization subroutine.
  182.  
  183.  my $dialog = $main->Dialog(-text => 'Exit OptiPerl?',
  184.                               -bitmap => 'question',
  185.                               -title => 'Demo Plug-IN',
  186.                               -default_button => 'Yes',
  187.                               -buttons => [qw/Yes No/]);
  188.  
  189.  $win->Show;
  190.  #Show the window, because the user might have minimized it
  191.  my $winhandle = $optiperl->GetWindowHandle($win);
  192.  #Get handle to the window that optiperl assigned to the plug-in
  193.  $optiperl->GrabWindow($plug_id,1,$winhandle);
  194.  #Disable input to optiperl, and tell it to keep this window
  195.  #in the foreground
  196.  my $answer = $dialog->Show;
  197.  $optiperl->GrabWindow($plug_id,0,0);
  198.  #Enable input in optiperl
  199.  return ($answer eq 'Yes');
  200. }
  201.  
  202. sub OnDocumentClose {
  203.  AddLog("Closed: $_[0]");
  204. }
  205.  
  206. sub OnBeforeAction {
  207.  AddLog("Before: $_[0]");
  208.  #Returning 0 will cancel executing the action.
  209.  return 1;
  210. }
  211.  
  212. sub OnAfterAction {
  213.  AddLog("After: $_[0]");
  214. }
  215.  
  216. sub OnParseStart {
  217.  AddLog("OnParseStart");
  218. }
  219.  
  220. sub OnParseEnd {
  221.  AddLog("OnParseEnd");
  222. }
  223.  
  224. #  sub OnKeyEvent {
  225. #  # A very low level key handler.
  226. #   AddLog("Key $_[0] $_[1]");
  227. #   return 1;
  228. #   #returning 0 will cancel the key event!
  229. #  }
  230.  
  231. sub IApplication_Test {
  232.  
  233.  my $handle = $optiperl->Handle;
  234.  #return a windows handle to optiperl application (if you need something like this)
  235.  
  236.  $optiperl -> OutputClear;
  237.  #Clears the text in the "text" tab of the browser window
  238.  $optiperl -> OutputAddLine("IApplication Test");
  239.  #add a line in the "text" tab of the browser window
  240.  
  241.  $optiperl -> StatusBarText("Hello from this plug in");
  242.  #set a string in the status bar
  243.  
  244.  $optiperl -> ExecuteAction("SelectLineAction");
  245.  #Executes an action within optiperl. For a list of possible values, see the
  246.  #file Options.ohk
  247.  
  248.  $BackgroundColor = $optiperl -> GetOpt("EditorColor");
  249.  # The SetOpt & GetOpt methods controls optiperl's options
  250.  $optiperl -> SetOpt("EditorColor",100000);
  251.  $optiperl -> UpdateOptions(1);
  252.  #Tell optiperl to update options that have to do with visible elements
  253.  $optiperl -> SetOpt("EditorColor",$BackgroundColor);
  254.  $optiperl -> UpdateOptions(1);
  255. }
  256.  
  257.  
  258. sub IDocument_Test {
  259.  my $DocCount = $optiperl->DocumentCount;
  260.  #Number of documents open in optiperl's editor
  261.  my $doc0 = $optiperl->Documents(0);
  262.  #Returns an IDocument with the first document open.
  263.  my $linecount = $doc0->LineCount;
  264.  
  265.  $optiperl->NewDocument("New Test.pl");
  266.  my $doc= $optiperl->ActiveDocument;
  267.  #Returns an IDocument with the document currently being edited.
  268.  $doc->Add("Test line 2");
  269.  $doc->Insert(0,"Test line 1");
  270.  $doc->Add("Test line 3");
  271.  $doc->Delete(2);
  272.  my $line = $doc->Lines(0);
  273.  $doc->{CursorPosX}=0;
  274.  $doc->{CursorPosY}=0;
  275.  $doc->TempHightlightLine(0);
  276. }
  277.  
  278. sub IProject_Test {
  279.  my $project = $optiperl->Project;
  280.  #Return an IProject with OptiPerl's project
  281.  $project->NewProject('c:\perl pod files.prj');
  282.  $project->AddFile('c:\perl\lib\pod\perl.pod');
  283.  $project->AddFolder('c:\perl\lib\pod','*.pod');
  284.  $project->SetOpt('LocalPath','c:\perl\lib\pod');
  285.  $project->UpdateOptions;
  286.  
  287.  my $count = $project->Count;
  288.  #Number of files in project
  289.  
  290.  AddLog($count);
  291.  
  292.  for (my $i = 0; $i < $count; $i++) {
  293.   $item = $project->items($i);
  294.   AddLog($item->Filename);
  295.  }
  296.  
  297.  # note that if we add a new item with AddFile, it will not be necessarily at
  298.  # $count number. Here is how to add or find an item and get it's options:
  299.  
  300.   $item = $project->AddFile('c:\perl\lib\pod\perl.pod');
  301.   $item->{mode} = 755;
  302.  
  303.   $optiperl->ExecuteAction("ShowManagerAction");
  304.   #show project manager window
  305. }
  306.  
  307. sub ICodeExplorer_Test {
  308.  my $treeview = $optiperl->CodeExplorer;
  309.  #Return an ITreeview with OptiPerl's code explorer
  310.  if (! defined $newnode) {
  311.      $newnode = $treeview->
  312.           AddNode($treeview->RootNode);
  313.      $newnode->{Caption}="Test Node";
  314.      for (my $i=0; $i<10; $i++)
  315.      {
  316.       my $node = $treeview->AddNode($newnode);
  317.       $node->{Caption}="Node $i";
  318.      }
  319.  }
  320. }
  321.  
  322. sub Window_Test {
  323.  if (! defined $win2) {
  324.   $top = $main->Toplevel;
  325.   $top->Label(-text => 'Another Window')->pack;
  326.   $win2 = $optiperl->RequestWindow($plug_id);
  327.   $win2->Show;
  328.   $win2->{Title} = "Plug-in Demo Second modal window";
  329.   ProcessEvents();
  330.   $optiperl->DockWindow($plug_id, hex $top->winfo("id"), $win2);
  331.  }
  332.  $win2->Show;
  333. }
  334.  
  335. sub Big_Test {
  336.  for (my $i=1; $i<=10; $i++)
  337.  {
  338.   ICodeExplorer_Test();
  339.   IApplication_Test();
  340.   IProject_Test();
  341.   IDocument_Test();
  342.   $optiperl->CodeExplorer->DeleteNode($newnode);
  343.   $optiperl->CloseDocument;
  344.  }
  345. }
  346.  
  347. sub MessageBoxTest {
  348.  my $string = $optiperl->InputBox('Plug in','Enter Text:','Default');
  349.  $optiperl->OutputAddLine($string);
  350.  
  351.  my $MB_OK = 0;
  352.  my $MB_OKCANCEL = 1;
  353.  my $MB_ABORTRETRYIGNORE = 2;
  354.  my $MB_YESNOCANCEL = 3;
  355.  my $MB_YESNO = 4;
  356.  my $MB_RETRYCANCEL = 5;
  357.  
  358.  my $MB_ICONHAND = 0x10;
  359.  my $MB_ICONQUESTION = 0x20;
  360.  my $MB_ICONEXCLAMATION = 0x30;
  361.  my $MB_ICONASTERISK = 0x40;
  362.  
  363.  my $res=$optiperl->MessageBox("Plug-In","Message Box",$MB_OKCANCEL + $MB_ICONEXCLAMATION);
  364.  $optiperl->OutputAddLine($res);
  365. }
  366.  
  367.  
  368. sub AddLog {
  369.  $log -> insert("end", $_[0]);
  370.  $log -> see("end");
  371. }
  372.  
  373. # Debugging section.
  374. # A plug-in must not have any code in the main block.
  375. # However we can add some code here to run our subroutines
  376. # while developing our plug-in (so we can run it by pressing
  377. # "Run in console" in OptiPerl or debug it).
  378.  
  379. # This can be done by checking if the magic variable
  380. # $valid_plugin is NOT defined.
  381.  
  382. # There are however some limitations, for example
  383. # you cannot dock the window into optiperl.
  384.  
  385. if (! defined $valid_plugin)
  386. {
  387.  sub ProcessEvents {};
  388.  Initialization;
  389.  MainLoop;
  390. }